# coding: utf-8

import networkx as nx
import matplotlib.pyplot as plt

class ImgRender(object):
    def __init__(self, meta):
        self.meta = meta
        self.graphs = {}



    def get_graph(self, name):
        g = self.graphs.get(name)
        if g:
            return g
        g = nx.DiGraph()
        self.graphs[name] = g
        return g


    def add_nodes(self, ref):
        self.add_node(ref.name)
        for refs in ref.targets.values():
            for name in refs:
                self.add_node(name)

    def add_node(self, name):
        if name in self.nodelist:
            return
        self.nodelist.append(name)

    def render(self, f):

        for ref in self.meta.references:
            for name, dests in ref.targets.items():
                g = self.get_graph(name)
                for d in dests:
                    g.add_edge(ref.name, d)

        G = self.get_graph('interface')


        pos=nx.spring_layout(G,iterations=10)
        nx.draw(G,pos,node_size=1,alpha=0.4,edge_color='r',font_size=11)
        plt.savefig("path.png")
        #plt.savefig(f)